﻿module PrintUtils

open Ast
open Lexer
open Parser
open System.IO
open System

let token_to_string (t:token) = 
  match t with 
  | EOF  -> "EOF" 
  | PLUS (line, column) -> "PLUS" + " at line: " + (string line) + ", column: " + (string column)
  | MINUS  (line, column) -> "MINUS"  + " at line: " + (string line) + ", column: " + (string column)
  | MUL  (line, column) -> "MUL"  + " at line: " + (string line) + ", column: " + (string column)
  | DIV  (line, column) -> "DIV"  + " at line: " + (string line) + ", column: " + (string column)
  | MOD  (line, column) -> "MOD"  + " at line: " + (string line) + ", column: " + (string column)
  | AND  (line, column) -> "AND"  + " at line: " + (string line) + ", column: " + (string column)
  | OR  (line, column) -> "OR"  + " at line: " + (string line) + ", column: " + (string column)
  | LOWER  (line, column) -> "LOWER"  + " at line: " + (string line) + ", column: " + (string column)
  | LOWEREQ  (line, column) -> "LOWEREQ"  + " at line: " + (string line) + ", column: " + (string column)
  | GREATER  (line, column) -> "GREATER"  + " at line: " + (string line) + ", column: " + (string column)
  | GREATEREQ  (line, column) -> "GREATEREQ"  + " at line: " + (string line) + ", column: " + (string column)
  | EQ  (line, column) -> "EQ"  + " at line: " + (string line) + ", column: " + (string column)
  | NOTEQ  (line, column) -> "NOTEQ"  + " at line: " + (string line) + ", column: " + (string column)
  | NOT  (line, column) -> "NOT"  + " at line: " + (string line) + ", column: " + (string column)
  | ASSIGN  (line, column) -> "ASSIGN"  + " at line: " + (string line) + ", column: " + (string column)
  | CLASS  (line, column) -> "CLASS"  + " at line: " + (string line) + ", column: " + (string column)
  | EXTENDS  (line, column) -> "EXTENDS"  + " at line: " + (string line) + ", column: " + (string column)
  | VOID  (line, column) -> "VOID"  + " at line: " + (string line) + ", column: " + (string column)
  | TY_INT  (line, column) -> "TY_INT"  + " at line: " + (string line) + ", column: " + (string column)
  | TY_BOOLEAN  (line, column) -> "TY_BOOLEAN"  + " at line: " + (string line) + ", column: " + (string column)
  | TY_STRING  (line, column) -> "TY_STRING"  + " at line: " + (string line) + ", column: " + (string column)
  | RETURN  (line, column) -> "RETURN"  + " at line: " + (string line) + ", column: " + (string column)
  | IF  (line, column) -> "IF"  + " at line: " + (string line) + ", column: " + (string column)
  | ELSE  (line, column) -> "ELSE"  + " at line: " + (string line) + ", column: " + (string column)
  | UAIL  (line, column) -> "UAIL"  + " at line: " + (string line) + ", column: " + (string column)
  | WHILE  (line, column) -> "WHILE"  + " at line: " + (string line) + ", column: " + (string column)
  | BREAK  (line, column) -> "BREAK"  + " at line: " + (string line) + ", column: " + (string column)
  | CONTINUE  (line, column) -> "CONTINUE"  + " at line: " + (string line) + ", column: " + (string column)
  | NEW  (line, column) -> "NEW"  + " at line: " + (string line) + ", column: " + (string column)
  | LENGTH  (line, column) -> "LENGTH"  + " at line: " + (string line) + ", column: " + (string column)
  | TRUE  (line, column) -> "TRUE"  + " at line: " + (string line) + ", column: " + (string column)
  | FALSE  (line, column) -> "FALSE"  + " at line: " + (string line) + ", column: " + (string column)
  | NULL  (line, column) -> "NULL"  + " at line: " + (string line) + ", column: " + (string column)
  | THIS  (line, column) -> "THIS"  + " at line: " + (string line) + ", column: " + (string column)
  | BRA  (line, column) -> "BRA"  + " at line: " + (string line) + ", column: " + (string column)
  | KET  (line, column) -> "KET"  + " at line: " + (string line) + ", column: " + (string column)
  | SQBRA  (line, column) -> "SQBRA"  + " at line: " + (string line) + ", column: " + (string column)
  | SQKET  (line, column) -> "SQKET"  + " at line: " + (string line) + ", column: " + (string column)
  | CURBRA  (line, column) -> "CURBRA"  + " at line: " + (string line) + ", column: " + (string column)
  | CURKET  (line, column) -> "CURKET"  + " at line: " + (string line) + ", column: " + (string column)
  | SEMICOLON  (line, column) -> "SEMICOLON"  + " at line: " + (string line) + ", column: " + (string column)
  | COMMA  (line, column) -> "COMMA"  + " at line: " + (string line) + ", column: " + (string column)
  | DOT  (line, column) -> "DOT"  + " at line: " + (string line) + ", column: " + (string column)
  | STRING _ -> "STRING" 
//  | UID s -> "UID : " + s
//  | LID s -> "LID : " + s
  | ULID s -> "ULID : " + s
  | BOOLEAN n -> "BOOLEAN : " + string n
  | FLOAT n -> "FLOAT : " + string n
  | INT32 n -> "INT32 : " + string n

let rec find_tokens w =
  try 
    match Lexer.tokenize w with
    | token.EOF -> ()
    | alpha ->
      Console.WriteLine(token_to_string alpha)
      find_tokens w
  with ex ->
    let error_line = sprintf "Error in line %d, column %d; not recognised;" w.EndPos.Line w.EndPos.Column
    do Console.WriteLine(error_line)
    do Console.ReadLine() |> ignore